﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using AnimEditor.Anim;
using AnimEditor.Define;
using System.Drawing;
using AnimEditor.Tile;

namespace AnimEditor.Picture
{
  public class AnimFrameTreeNode : TreeNode
  {
    AnimFrameData mFrameData;

    public AnimFrameTreeNode(AnimFrameData framedata)
    {
      mFrameData = framedata;
      mFrameData.TreeNode = this;
      MenuItem[] AnimTreeNodeMenuItems = new MenuItem[] 
      {
        new MenuItem("删除帧", AnimFrameTreeNodeMenuItems_DelAFrame_click)
      };
      //AnimTreeNodeMenuItems[0] = new ;
      this.ContextMenu = new ContextMenu(AnimTreeNodeMenuItems);
    }
    public AnimFrameData FrameData
    {
      get { return mFrameData; }
      set { mFrameData = value; }
    }
    void AnimFrameTreeNodeMenuItems_DelAFrame_click(object sender, EventArgs e)
    {
		DeleteNode();
    }
	public void DeleteNode()
	{
		AnimTreeNode parentnode = this.Parent as AnimTreeNode;

		parentnode.DelAnimFrame(this);

		for (int i = 0; i < parentnode.Nodes.Count; ++i)
		{
			TreeNode node = parentnode.Nodes[i];
			node.Text = string.Format("Frame{0}", i);
		}
		PicMgr.Instance.SelTreeNode = this.Parent;
	}
    public void Clear()
    {
		mFrameData.Clear();
		mFrameData = null;
    }
  }

  public class AnimTreeNode : TreeNode
  {
    AnimData mData;
   
    public AnimTreeNode(AnimData data) : base(data.Name)
    {
      mData = data;
      mData.TreeNode = this;
      Text = mData.Name;
      MenuItem[] AnimTreeNodeMenuItems = new MenuItem[]
      {
        new MenuItem("删除动画",AnimTreeNodeMenuItems_DelAnim_click),
      };
      this.ContextMenu = new ContextMenu(AnimTreeNodeMenuItems);
    }
    public AnimData Data  
    {
      get { return mData; }
    }
    public void DelAnimFrame(AnimFrameTreeNode delnode)
    {
      Nodes.Remove(delnode);
      Data.AnimFrame.Remove(delnode.FrameData);
	  delnode.Clear();
    }

    void AnimTreeNodeMenuItems_DelAnim_click(object sender, EventArgs e)
    {
      while(Nodes.Count!=0)
      {
        DelAnimFrame(Nodes[0] as AnimFrameTreeNode);
      }
      this.TreeView.Nodes.Remove(this);
      AnimMgr.Instance.AnimDataLst.Remove(mData);
      mData.TreeNode = null;
      mData = null;
      PicMgr.Instance.SelTreeNode = null;
    }
    public void Clear()
    {
      mData = null;
      for(int i=0;i<Nodes.Count;i++)
      {
        AnimFrameTreeNode node = (AnimFrameTreeNode)Nodes[i];
        node.Clear();
      }
    }
  }
  
}
